package com.xwb.controller;

import com.xwb.entity.User;
import com.xwb.entity.Warehouse;
import com.xwb.mapper.UserMapper;
import com.xwb.mapper.WarehouseMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ToController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;

    @RequestMapping({"/","/toLoginPage"})
    public String toLoginPage(){
        return "loginPage";
    }

    @RequestMapping("/toLoginScussessPage")
    public String toLoginScussessPage(){
        //获取当前的用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user.getAdministrator()==1) return "administratorIndexPage";
        else return "userIndexPage";
    }

    @RequestMapping("/toUserIndexPage")
    @RequiresRoles("0")
    public String toUserIndexPage(){
        //System.out.println(((org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
        return "userIndexPage";
    }

    @RequestMapping("/toRegisterPage")
    public String toRegisterPage(){
        return "registerPage";
    }

    @RequestMapping("/toAdmiIndexPage")
    @RequiresRoles("1")
    public String toAdministratorIndexPage(){
        return "administratorIndexPage";
    }

    @RequestMapping("/toadmiQueryWareHousePage")
    @RequiresRoles("1")
    public String toadmiQueryWareHousePage(){
        return "toadministratorQueryWareHousePage";
    }

    @RequestMapping("/toAdmiQueryUserPage")
    @RequiresRoles("1")
    public String toAdmiQueryUserPage(){
        return "admiQueryUserPage";
    }

    @RequestMapping("/toUserQueryAllInstoragePage")
    @RequiresRoles("0")
    public String toUserQueryAllInstoragePage(){
        return "userQueryAllInstoragePage";
    }

    @RequestMapping("/toUserQueryAllOutstoragePage")
    @RequiresRoles("0")
    public String toUserQueryAllOutstoragePage(){
        return "userQueryAllOutstoragePage";
    }

    @RequestMapping("/toAdmiQueryAllInstoragePage")
    @RequiresRoles("1")
    public String toAdmiQueryAllInstoragePage(){
        return "admiQueryAllInstoragePage";
    }

    @RequestMapping("/toAdmiQueryAllOutstoragePage")
    @RequiresRoles("1")
    public String toAdmiQueryAllOutstoragePage(){
        return "admiQueryAllOutstoragePage";
    }

    @RequestMapping("/toUserQueryWareHousePage")
    @RequiresRoles("0")
    public String toUserQueryWareHousePage(){
        return "userQueryWareHousePage";
    }

    @RequestMapping("/toInNewStoragePage")
    @RequiresRoles("0")
    public String toInNewStoragePage(){
        return "inNewStoragePage";
    }

    @RequestMapping("/toInOldStoragePage")
    @RequiresRoles("0")
    public String toInOldStoragePage(){
        return "inOldStoragePage";
    }

    @RequestMapping("/toOutStoragePage")
    @RequiresRoles("0")
    public String toOutStoragePage(){
        return "outStoragePage";
    }

    @RequestMapping("/toAdmiUpdateUserPage/{userId}")
    @RequiresRoles("1")
    public String toAdmiUpdateUserPage(@PathVariable("userId") Integer userId, Model model){
        User user = userMapper.selectById(userId);
        model.addAttribute("user",user);
        return "admiUpdateUserPage";
    }

    @RequestMapping("/toAdmiUpdateWareHousePage/{productId}")
    @RequiresRoles("1")
    public String toAdmiUpdateWareHousePage(@PathVariable("productId") Integer productId,Model model){
        Warehouse warehouse = warehouseMapper.selectById(productId);
        model.addAttribute("warehouse",warehouse);
        return "admiUpdateWareHousePage";
    }
}
